package com.mvc.web.order.dao.impl;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.mvc.web.common.ibatis.IbatisDaoImpl;
import com.mvc.web.order.dao.OrderDao;
import com.mvc.web.order.vo.FileVo;
import com.mvc.web.order.vo.GpsVo;
import com.mvc.web.order.vo.OrderForm;
import com.mvc.web.order.vo.OrderHis;
import com.mvc.web.order.vo.OrderVo;

@Repository
public class OrderDaoImpl extends IbatisDaoImpl implements OrderDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderVo> getList(OrderForm vo) {
		return (List<OrderVo>) queryForList("order.getList", vo);
	}

	@Override
	public long getCount(OrderForm vo) {
		return (long) queryForObject("order.getCount",vo);
	}

	@Override
	public int create(OrderVo vo) {
		return (int) insert("order.create", vo);
	}

	@Override
	public int createHis(OrderHis vo) {
		return (int) insert("order.createHis", vo);
	}

	@Override
	public void commit(Integer orderId, int uid,Integer carOwnerId) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("orderId", orderId);
		map.put("uid", uid);
		map.put("carOwnerId", carOwnerId);
		map.put("mtime", System.currentTimeMillis()/1000);
		update("order.commit",map);
	}

	@Override
	public int getCountForValidate(String phone) {
		return (int) queryForObject("order.getCountForValidate", phone);
	}

	@Override
	public void deleteFile(String fileId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fileId", fileId);
		map.put("mtime", System.currentTimeMillis()/1000);
		update("order.deleteFile",map);
	}

	@Override
	public void uploadFile(FileVo vo) {
		insert("order.uploadFile", vo);
	}

	@Override
	public void update(OrderVo vo) {
		update("order.update",vo);
	}

	@Override
	public OrderVo getDetail(Integer orderId) {
		return (OrderVo) queryForObject("order.getDetail", orderId);
	}

	@Override
	public int getStatusById(Integer orderId) {
		return (int) queryForObject("order.getStatusById", orderId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FileVo> getFileList(Integer orderId, int mtype) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("orderId", orderId);
		map.put("materialType", mtype);
		return (List<FileVo>) queryForList("order.getFileList", map);
	}

	@Override
	public void delivery(Integer orderId, int uid, int status) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("orderId", orderId);
		map.put("uid", uid);
		map.put("status", status);
		map.put("mtime", System.currentTimeMillis()/1000);
		update("order.delivery",map);
	}

	@Override
	public void gps(String longitude, String latitude, int uid,String address) {
		Map<String, Object> map = new Hashtable<String, Object>();
		map.put("longitude", longitude);
		map.put("uid", uid);
		map.put("latitude", latitude);
		map.put("address", address);
		map.put("mtime", System.currentTimeMillis()/1000);
		insert("order.gps",map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GpsVo> getGps(Integer orderId) {
		return (List<GpsVo>) queryForList("order.getGps", orderId);
	}
}
